package com.zyx.mpdemo.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author Yaxi.Zhang
 * @since 2023/2/22 10:55
 */
@Configuration
public class ThreadPoolConfig {

    private static final int NUMBER_OF_CORE = Runtime.getRuntime().availableProcessors();
    private static final long KEEP_ALIVE_TIME = 15L;
    private static final int CAPACITY = 30;

    @Bean(name = "transactionExecutor")
    public ExecutorService transactionExecutor() {
        ThreadFactory threadFactory = ThreadFactoryBuilder.create().setNamePrefix("transactionExecutor-").build();
        return new ThreadPoolExecutor(NUMBER_OF_CORE + 1, NUMBER_OF_CORE << 1, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(CAPACITY), threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
    }

}
